// 文件路径: src/main/java/com/example/demo22/mapper/LoginLogMapper.java

package com.example.demo22.mapper;

import com.example.demo22.entity.LoginLog;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

@Mapper
public interface LoginLogMapper {

    @Insert("INSERT INTO login_log (user_id, username, login_time, ip_address, user_agent) " +
            "VALUES (#{userId}, #{username}, #{loginTime}, #{ipAddress}, #{userAgent})")
    void insertLoginLog(@Param("userId") Long userId,
                        @Param("username") String username,
                        @Param("loginTime") Date loginTime,
                        @Param("ipAddress") String ipAddress,
                        @Param("userAgent") String userAgent);

    @Select("SELECT COUNT(*) FROM login_log")
    int countTotalLogins();

    @Select("SELECT COUNT(*) FROM login_log WHERE DATE(login_time) = CURDATE()")
    int countTodayLogins();

    @Select("SELECT * FROM login_log ORDER BY login_time DESC LIMIT #{limit}")
    List<LoginLog> getRecentLogins(@Param("limit") int limit);
}
